package controller

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service/goods"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

// 商品标签
type GoodsClass struct{}

// Set 设置标签
func (gc GoodsClass) Set(c *gin.Context) {
	var params struct {
		ClassID          string `json:"class_id"`
		ClassName        string `json:"class_name"`
		ClassDescription string `json:"class_description"`
		MerchantID       string `json:"merchant_id"`
	}
	err := c.ShouldBindJSON(&params)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	params.MerchantID = c.GetString("merchant_id")
	db := mysql.NewDB()
	err = goods.GoodsClassService{}.Set(c.Request.Context(), db, params.ClassID, params.ClassName, params.ClassDescription, params.MerchantID)
	response.JSON(c, nil, err)
}

// List 获取标签列表
func (gc GoodsClass) List(c *gin.Context) {
	var params struct {
		Page       int    `json:"page"`
		Limit      int    `json:"limit"`
		MerchantID string `json:"merchant_id"`
	}
	err := c.ShouldBindJSON(&params)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	params.MerchantID = c.GetString("merchant_id")
	db := mysql.NewDB()
	data, count, err := goods.GoodsClassService{}.List(c.Request.Context(), db, params.MerchantID, params.Page, params.Limit)
	response.JSON(c, gin.H{
		"data":  data,
		"count": count,
	}, err)
}

// Remove 删除商品标签
func (gc GoodsClass) Remove(c *gin.Context) {
	var params struct {
		ClassID string `json:"class_id"`
	}
	err := c.ShouldBindJSON(&params)
	if err != nil {
		response.JSON(c, nil, errorx.New("参数错误", -1))
		return
	}

	MerchantID := c.GetString("merchant_id")
	db := mysql.NewDB()
	err = goods.GoodsClassService{}.Remove(c.Request.Context(), db, MerchantID, params.ClassID)
	response.JSON(c, nil, err)

}
